//
//  TJQuickTradingController.m
//  Teleport
//
//  Created by hc-101 on 2020/6/17.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJQuickTradingController.h"
 
@interface TJQuickTradingController ()<UIGestureRecognizerDelegate>

@property (strong, nonatomic) NSMutableArray *datas;
@property (weak, nonatomic) UIView *maskView;
@property (weak, nonatomic) UIView *contentView;
@property (weak, nonatomic) UIButton *amountBuyButton;
@property (weak, nonatomic) UIButton *numberBuyButton;
@property (weak, nonatomic) UITextField *numberTextField;
@property (weak, nonatomic) UIButton *buyButton;
/** 1金额 2数量*/
@property (copy, nonatomic) NSString *buyType;
@property (weak, nonatomic) UILabel *limitLabel;
@property (copy, nonatomic) NSString *coinName;
@property (weak, nonatomic) UILabel *unitLabel;
@property (copy, nonatomic) NSString *unitSymbol;
@property (copy, nonatomic) NSString *unit;
@property (copy, nonatomic) NSString *buySell;

@property (weak, nonatomic) UILabel *totalAmountValueLabel;
@property (weak, nonatomic) UILabel *numberLabel;

@end

  
@implementation TJQuickTradingController



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = UIColor.tj_alertMaskColor;
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
    } completion:^(BOOL finished) {
    }];
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _datas = @[].mutableCopy;
 
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIView *maskView = [UIView tj_addToView:self.view];
    maskView.backgroundColor = [UIColor.tj_alertMaskColor colorWithAlphaComponent:0];
    maskView.frame = self.view.bounds;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickDismissButton)];
    [maskView addGestureRecognizer:tap];
    tap.delegate = self;
    
    UIView *contentView = [UIView tj_addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    UIView *headerView = [UIView tj_addToView:contentView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(60);
        make.top.offset(0);
    }];
 
 
    _buySell =  [_model.entrustType isEqualToString:@"1"] ? @"购买".localizedString : @"出售".localizedString;
    _coinName = _model.coinNo;
    _unitSymbol = _model.moneyType.intValue == 1 ? @"¥": @"€";
    _unit = _model.moneyType.intValue == 2 ? @"EUR" : @"CNY";
    
    UILabel *titleLabel = [UILabel lableWithSuperView:headerView font:MediumFont(16) color:UIColor_Hex(0x272727) title:[NSString stringWithFormat:@"%@%@", _buySell, _coinName] textAlignment:NSTextAlignmentLeft];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10);
        make.left.offset(16);
    }];
    
    
    UILabel *priceLabel = [UILabel lableWithSuperView:headerView font:UIFont_Px(13) color:UIColor_Hex(0x333333) title:[NSString stringWithFormat:@"单价¥%@".localizedString, _model.entrustPrice] textAlignment:NSTextAlignmentLeft];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(6);
        make.left.equalTo(titleLabel);
    }];
    
    UIView *lineView = [UIView tj_addToView:headerView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.right.offset(-16);
        make.bottom.offset(0);
        make.height.offset(0.7);
    }];
    
   
    UIButton *amountBuyButton = [UIButton buttonWithSuperView:contentView font:UIFont_Px(14) color:UIColor_Hex(0x262626) title:[NSString stringWithFormat:@"按金额%@".localizedString, _buySell]];
    [amountBuyButton setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    [amountBuyButton setImage:[UIImage imageNamed:@"select_icon"] forState:UIControlStateHighlighted];
    [amountBuyButton setImage:[UIImage imageNamed:@"select_icon"] forState:UIControlStateSelected];
    [amountBuyButton setTitleColor:UIColor_Hex(0x3881E8) forState:UIControlStateSelected];
    [amountBuyButton addTarget:self action:@selector(didClickAmountBuyButton:) forControlEvents:UIControlEventTouchUpInside];
    [amountBuyButton setImagePosition:LXMImagePositionLeft spacing:6];
    [amountBuyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(100);
        make.left.offset(16);
        make.top.equalTo(headerView.mas_bottom).offset(20);
        make.height.offset(30);
    }];
    
    UIButton *numberBuyButton = [UIButton buttonWithSuperView:contentView font:UIFont_Px(14) color: UIColor_Hex(0x262626) title:[NSString stringWithFormat:@"按数量%@".localizedString, _buySell]];
    [numberBuyButton setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    [numberBuyButton setImage:[UIImage imageNamed:@"select_icon"] forState:UIControlStateHighlighted];
    [numberBuyButton setImage:[UIImage imageNamed:@"select_icon"] forState:UIControlStateSelected];
    [numberBuyButton setTitleColor:UIColor_Hex(0x3881E8) forState:UIControlStateSelected];
    [numberBuyButton addTarget:self action:@selector(didClickNumberBuyButton:) forControlEvents:UIControlEventTouchUpInside];
    [numberBuyButton setImagePosition:LXMImagePositionLeft spacing:6];
    [numberBuyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(100);
        make.left.equalTo(amountBuyButton.mas_right).offset(30);
        make.centerY.equalTo(amountBuyButton);
        make.height.offset(30);
    }];
 
    
    UIView *buyContentView = [UIView tj_addToView:contentView];
    buyContentView.backgroundColor = UIColor_Hex(0xF2F2F2);
    [buyContentView shearRoundedCornersWithRadiu:3];
    [buyContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(amountBuyButton.mas_bottom).offset(16);
        make.left.offset(16);
        make.right.offset(-16);
        make.height.offset(40);
    }];
 
    UITextField *numberTextField = [UITextField tj_addToView:buyContentView];
    numberTextField.placeholder = [NSString stringWithFormat:@"请输入%@金额".localizedString, _buySell];
    numberTextField.font = UIFont_Px(15);
    [numberTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    numberTextField.keyboardType = UIKeyboardTypeDecimalPad;
    [numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.centerY.offset(0);
        make.height.offset(34);
        make.width.offset(200);
    }];
    
    UILabel *allLabel = [UILabel lableWithSuperView:buyContentView font:UIFont_Px(11) color:UIColor_Hex(0x262626) title:[NSString stringWithFormat:@"全部%@".localizedString, [_model.entrustType isEqualToString:@"1"] ? @"买入".localizedString : @"出售".localizedString] textAlignment:NSTextAlignmentCenter];
    [allLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(0);
        make.height.offset(30);
        make.width.offset(70);
    }];
                         
     
    UIView *allLineView = [UIView tj_addToView:buyContentView];
    allLineView.backgroundColor = UIColor.tj_separatorColor;
    [allLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(30);
        make.right.equalTo(allLabel.mas_left);
        make.centerY.offset(0);
        make.width.offset(0.7);
    }];
    
    UILabel *unitLabel = [UILabel lableWithSuperView:buyContentView font:UIFont_Px(11) color:UIColor_Hex(0x262626) title:@"CNY" textAlignment:NSTextAlignmentCenter];
    [unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.equalTo(allLineView.mas_left);
        make.height.offset(30);
        make.height.offset(60);
    }];
    
    UILabel *limitLabel = [UILabel lableWithSuperView:contentView font:UIFont_Px(11) color:UIColor_Hex(0x999999) title:[NSString stringWithFormat:@"限额¥%@~¥%@".localizedString, _model.entrustMinPrice, _model.entrustMaxPrice] textAlignment:NSTextAlignmentLeft];
    [limitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(buyContentView.mas_bottom);
        make.left.equalTo(titleLabel);
        make.height.offset(30);
    }];
    
    UILabel *numberLabel = [UILabel lableWithSuperView:contentView font:UIFont_Px(12) color:UIColor_Hex(0x262626) title:[NSString stringWithFormat:@"交易数量%@%@".localizedString, _model.remainNum, _coinName] textAlignment:NSTextAlignmentRight];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(limitLabel.mas_bottom).offset(10);
        make.right.equalTo(buyContentView);
        make.height.offset(30);
    }];
    
    UILabel *totalAmountLabel = [UILabel lableWithSuperView:contentView font:UIFont_Px(14) color:UIColor_Hex(0x333333) title:[_model.entrustType isEqualToString:@"1"] ? @"实付款".localizedString : @"实收款".localizedString textAlignment:NSTextAlignmentRight];
    [totalAmountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(numberLabel.mas_bottom).offset(10);
        make.left.equalTo(titleLabel);
    }];
    
    UILabel *totalAmountValueLabel = [UILabel lableWithSuperView:contentView font:MediumFont(15) color:UIColor_Hex(0x3881E8) title:@"¥0.00" textAlignment:NSTextAlignmentRight];
    [totalAmountValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(totalAmountLabel);
        make.right.equalTo(buyContentView);
    }];
    
    
    UIButton *buyButton = [UIButton buttonWithSuperView:contentView font:UIFont_Px(15) color:UIColor.whiteColor title:@"下单".localizedString];
    buyButton.backgroundColor = UIColor.tj_mainColor;
    [buyButton shearRoundedCornersWithRadiu:18];
    [buyButton addTarget:self action:@selector(didClickBuyButton) forControlEvents:UIControlEventTouchUpInside];
    [buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(200);
         make.height.offset(36);
        make.centerX.offset(0);
        make.top.equalTo(totalAmountLabel.mas_bottom).offset(25);
    }];
 
    CGFloat totalHeight = 320 + TabbarSafeBottomMargin + 50;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, totalHeight);
    

    _numberLabel = numberLabel;
    _maskView = maskView;
    _contentView = contentView;
    _unitLabel = unitLabel;
    _buyButton = buyButton;
    _numberBuyButton = numberBuyButton;
    _amountBuyButton = amountBuyButton;
    _numberTextField = numberTextField;
    _limitLabel = limitLabel;
    _totalAmountValueLabel = totalAmountValueLabel;
    
    [self didClickAmountBuyButton:amountBuyButton];
}
 
- (void)textFieldDidChange:(UITextField *)textField {
    _buyButton.userInteractionEnabled = _numberTextField.text.length;
    _buyButton.backgroundColor = (_buyButton.userInteractionEnabled) ? UIColor.tj_mainColor : [UIColor_Hex(0x666666) colorWithAlphaComponent:0.3];
 
    if (_buyType.intValue == 1) {//金额
        _totalAmountValueLabel.text = [NSString stringWithFormat:@"%@%@", _unitSymbol, [Tools CNYString:_numberTextField.text]];
        _numberLabel.text = [NSString stringWithFormat:@"%@%@%@", @"交易数量".localizedString, [Tools reviseString:[NSString stringWithFormat:@"%f", _numberTextField.text.floatValue / _model.entrustPrice.floatValue]], _coinName];
    } else {
        _totalAmountValueLabel.text = [NSString stringWithFormat:@"%@%@", _unitSymbol, [Tools CNYString:[NSString stringWithFormat:@"%f", _numberTextField.text.floatValue * _model.entrustPrice.floatValue]]];
        
        _numberLabel.text = [NSString stringWithFormat:@"%@%@%@",@"交易数量".localizedString, [Tools reviseString:_numberTextField.text], _coinName];

    }
    
    
}
 
- (void)didClickAmountBuyButton:(UIButton *)button {
    button.selected = !button.selected;
    _numberBuyButton.selected = !button.selected;
    
    if (button.selected) {
        _numberTextField.placeholder = @"请输入金额".localizedString;
        self.buyType = @"1";
        _limitLabel.text = [NSString stringWithFormat:@"限额%@~%@".localizedString,[NSString stringWithFormat:@"%@%@",_unitSymbol, [Tools CNYString:_model.entrustMinPrice]], [NSString stringWithFormat:@"%@%@", _unitSymbol, [Tools CNYString:_model.entrustMaxPrice]]];
        _unitLabel.text = _unit;
    }
    
    [self textFieldDidChange: _numberTextField];
}

- (void)didClickNumberBuyButton:(UIButton *)button {
    button.selected = !button.selected;
    _amountBuyButton.selected = !button.selected;
    
    if (button.selected) {
        _numberTextField.placeholder = @"请输入数量".localizedString;
        self.buyType = @"2";
        NSString *maxValue = [Tools reviseString:[NSString stringWithFormat:@"%f", _model.entrustMaxPrice.floatValue / _model.entrustPrice.floatValue]];
        NSString *minValue = [Tools reviseString:[NSString stringWithFormat:@"%f", _model.entrustMinPrice.floatValue / _model.entrustPrice.floatValue]];
        _limitLabel.text = [NSString stringWithFormat:@"限量%@%@~%@%@".localizedString,minValue,_coinName, maxValue, _coinName];
        _unitLabel.text = _coinName;
    }
    
    [self textFieldDidChange: _numberTextField];

}
#pragma mark ---------------UIGestureRecognizerDelegate------------------
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (touch.view != self.maskView) {
        return NO;
    } else {
        return YES;
    }
}


- (void)didClickDismissButton {
    [self dismissComplete:nil];
}

- (void)didClickBuyButton {
    
    [self dismissComplete:^{
        if (self.selectComplete) {
            self.selectComplete(self.numberTextField.text, self.buyType);
        }
    }];
}

- (void)dismissComplete:(void (^)(void))complete {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) complete();
        }];
    }];
}





@end
